<?php

/**
 * composite : Extends baseField to create a composite field
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Fields_Base_Composite extends Erra_Fields_Base_BaseField {

    protected $_subFields = array();

    function __construct($objectId=null) {
        parent::__construct($objectId);
        $this->setDatabaseType('text', 0);
    }

    /**
     * Fill subfields with rawData
     *
     * @param mixed $data_array : An associative array which contains rawData for subfields (an array with non-numerical keys only)
     * @return
     */
    public function fillSubFields(Array $data_array) {

        foreach ($this->_subFields as $aSubField) {
            if (isset($data_array[$aSubField->getObjectId()])) {
                $newData = Erra_Serialization_SerializerJson::unserializeRaw($data_array[$aSubField->getObjectId()]);
                $aSubField->setRawData($newData);
            }
        }
        return $this;
    }

    /**
     * composite::getSubFields() : Get list of subfields in an array
     *
     * @return
     */
    function getSubFields($bRealFields = false) {
        if($bRealFields == false) {
            return $this->_subFields;
        }
        $resultArray = array();
        foreach($this->_subFields as $aSubField) {
            if(!strstr(get_class($aSubField), 'Erra_Fields_Search')) {
                $resultArray[] = $aSubField;
            }
        }
        return $resultArray;
    }

    /**
     * composite::getSubFieldCount() : Get subfields count
     *
     * @return - Le nombre de sous champs
     */
    function getSubFieldCount() {
        return count($this->_subFields);
    }

    /**
     * composite::addField() : Adds a subfield to current field
     *
     * @param mixed $field - The subfield you want to had. If the subfield has an id which already exists, it will be overwritten.
     * @return
     */
    function addField($field) {
        if (get_class($field) == 'Erra_Fields_Callback') {
            $field->setParent($this);
        }
        $this->_subFields[$field->getObjectId()] = $field;
    }

    /**
     * composite::addFields($field_0,...,$field_n) : Add a list of subfields,
     *
     * @return Erra_Fields_Base_Field
     */
    function addFields() {
        $numargs = func_num_args();
        if (!$numargs)
            return;
        $args = func_get_args();
        foreach ($args as $field) {
            if (is_object($field)) {
                if (get_class($field) == 'Erra_Fields_Callback') {
                    $field->setParent($this);
                }
                $this->_subFields[$field->getObjectId()] = $field;
            }
        }
        return $this;
    }

    /**
     * composite::getSubFieldAt() : Returns subfields with the index $needle
     *
     * @param mixed $needle - The ID of the subfield you want to get back
     * @return Erra_Fields_Base_Field
     */
    public function getSubFieldAt($needle) {
        if (is_string($needle)) {
            if (is_array($this->_subFields) && isset($this->_subFields[$needle]))
                return $this->_subFields[$needle];
        }
        return null;
    }

    /**
     * composite::removeSubFieldAt() : Deletes subfield at specified index
     *
     * @param mixed $needle - The id of the subfield you want to delete
     * @return
     */
    public function removeSubFieldAt($needle) {
        if (isset($this->_subFields[$needle]))
            unset($this->_subFields[$needle]);
    }

    /**
     * composite::isComposite() : Check if field contains subfields
     *
     * @return - TRUE if field contains subfields, else it will return FALSE
     */
    public function isComposite() {
        return ($this->getSubFieldCount() == 0) ? false : true;
    }

    /** /
     *
     * @return boolean 
     */
    public function callbackExists() {
        foreach ($this->getSubFields() as $aSubField) {
            if (get_class($aSubField) == 'Erra_Fields_Callback') {
                return true;
            }
        }
        return false;
    }

}

?>